package com.mayikt;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

// 生产者
public class LongEventProducer {

    private RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer byteBuffer) {
        // 获取事件队列下标位置
        long sequence = ringBuffer.next();// 拿到环形的序列位置
        try {
            // 取出空队列（Event）
            LongEvent longEvent = ringBuffer.get(sequence);
            // 给空队列赋值
            longEvent.setValue(byteBuffer.getLong(0));
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            System.out.println("生产者发送数据...");
            // 发送数据
            ringBuffer.publish(sequence);
        }
    }
}